﻿using AnlianCommon.API.Commands.UserLogics;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Cottages
{
    public class CottageUpdateHander : IRequestHandler<CottageUpdateCommand, Result>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="personnelRepository"></param>
        /// <param name="mapper"></param>
        public CottageUpdateHander(IBaseRepository<Cottage> _cottageRepository, IMapper mapper)
        {
            this._cottageRepository = _cottageRepository;
            this._mapper = mapper;
        }

        /// <summary>
        /// 修改栋舍信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(CottageUpdateCommand request, CancellationToken cancellationToken)
        {
            //返回结果
            var result = new Result() { Code = (int)CommonErrorCode.Updated };
            try
            {
                //通过编号查询返回第一条数据
                var cottage = await(await _cottageRepository.GetAll()).Where(c => c.CottageId == request.CottageId).FirstOrDefaultAsync();
                //如果不为空查到数据那么就进行修改
                if (cottage != null)
                {
                    //审计字段修改人为空表示没有修改过用户名可以进行修改
                    if (cottage.UpdatedOwnerId == null)
                    {
                        cottage.UpdatedOwnerId = "李四";//审计字段修改人等于当前登录的用户
                        cottage.UpdatedTime = DateTime.Now;//审计字段修改时间等于当前系统时间
                        cottage.CottageTypeId = request.CottageTypeId;//更改cottageTypeId
                        cottage.CottageName = request.CottageName;//更改cottageName
                        cottage.PersId = request.PersId;//更改cottageArea
                        cottage.Production = request.Production;//更改cottageAddress
                        cottage.Status = request.Status;//更改cottageDesc
                        cottage.Ramark = request.Ramark;//更改cottageImgUrl
                        cottage.FlatId = request.FlatId;//更改cottageStatus
                        cottage.FarmId = request.FarmId;//更改cottageStatus
                        //定义变量调用修改方法
                        var res = await _cottageRepository.Update(cottage);
                        //受影响行数大于0表示修改成功
                        if (res > 0)
                        {
                            result.Code = (int)CommonErrorCode.Success;//返回成功响应码
                            result.Msg = "修改成功";//返回修改成功信息
                        }
                        //反之受影响行数小于或等于0表示修改失败
                        else
                        {
                            result.Code = (int)CommonErrorCode.Updated;//返回修改不成功响应码.
                            result.Msg = "修改失败";//返回修改失败信息
                        }
                    }
                    //反之审计字段修改人不为空代表，已经修改过了不可以修改
                    else
                    {
                        result.Msg = "已经修改过了,不可以修改了";//返回修改失败信息
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
            //最终返回结果
            return result;
        }
    }
}
